#include "chat.h"
#include <QMessageBox>
#include "ui_chat.h"

Chat::Chat(QString msg, QWidget *parent, QTcpSocket *cfd) :
    QMainWindow(parent),
    ui(new Ui::Chat)
{
    ui->setupUi(this);
    this->cfd = cfd; 
    this->ui->textEdit_msg->append(msg); 
    
    connect(this->cfd, &QTcpSocket::readyRead, this, [=]()
    {
       QString t = this->cfd->readAll(); 
       this->ui->textEdit_msg->append(t);  
    }); 
    
    connect(ui->btn_send, &QPushButton::clicked, this, [=]()
    {
        QString t = ui->textEdit_sendmsg->toPlainText(); 
        if(t.isEmpty()) return; 
        this->cfd->write(t.toUtf8()); 
        this->ui->textEdit_msg->append(t); 
        this->ui->textEdit_sendmsg->clear(); 
    }); 
}

Chat::~Chat()
{
    qDebug() << "Chat dead"; 
    delete ui;
}
